package com.heima.admin.services.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdChannelMapper;
import com.heima.admin.services.ChannelService;
import com.heima.model.admin.dtos.ChannelDto;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ChannelServiceImpl extends ServiceImpl<AdChannelMapper, AdChannel> implements ChannelService {
    @Autowired(required = false)
    public ResponseResult findByNameAndPage(ChannelDto dto){
        //检查参数
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //分页检查
        dto.checkParam();
        //封装查询条件
        IPage pageReq = new Page(dto.getPage(), dto.getSize());
        LambdaQueryWrapper<AdChannel> LambdaQueryWrapper = Wrappers.lambdaQuery();
        if (StringUtils.isNoneBlank(dto.getName())) {
            LambdaQueryWrapper.like(AdChannel::getName, dto.getName());
        }

        IPage<AdChannel> pageResult = this.page(pageReq, LambdaQueryWrapper);
        PageResponseResult pageResponseResult = new PageResponseResult(dto.getPage(),dto.getSize(),pageResult.getTotal());
        pageResponseResult.setData(pageResult.getRecords());
        return pageResponseResult;
    }

    @Override
    public ResponseResult insert(AdChannel channel) {
        if(channel == null || StringUtils.isBlank(channel.getName())){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道不能为空");
        }
        int count=  this.count(Wrappers.<AdChannel>lambdaQuery().eq(AdChannel::getName,channel.getName()));
        if(count>0){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"该频道已存在");
        }
        this.save(channel);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult update(AdChannel channel) {
        if(channel == null || channel.getId()==null) {
        ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道id不能为空");
        }
        AdChannel byId = this.getById(channel.getId());
        if(byId == null) {
           ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"频道数据不存在");
        }
        if(StringUtils.isBlank(channel.getName())||!channel.getName().equals(byId.getName())) {
          int count=  this.count(Wrappers.<AdChannel>lambdaQuery().eq(AdChannel::getName,channel.getName()));
            if(count>0){
                ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"该名称已存在");
            }
        }
        this.updateById(channel);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult delete(Integer id) {
        if(id==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道ID不能为空");
        }
        AdChannel channel = this.getById(id);
        if(channel==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        if(channel.getStatus()){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_ALLOW,"当前频道为启用状态，不可删除");
        }
        this.removeById(id);

//
        return ResponseResult.okResult();
    }
}
